﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;
using System;

namespace DevelopTool.Forms.DataConversionForms
{
    /// <summary>
    /// 时间换算
    /// </summary>
    public partial class TimeConversionForm : BaseUIPage
    {

        public TimeConversionForm()
        {
            InitializeComponent();
        }

        private void TimeConversionForm_Load(object sender, EventArgs e)
        {
            foreach (var control in this.uiTableLayoutPanel.Controls)
            {
                if (!(control is UITextBox textBox)) continue;

                textBox.GotFocus += UiTextBox_GotFocus;

                textBox.LostFocus += UiTextBox_LostFocus;
            }
        }

        /// <summary>
        /// 文本框获得焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UiTextBox_GotFocus(object sender, EventArgs e)
        {
            UITextBox uITextBox = sender as UITextBox;

            uITextBox.TextChanged += uiTextBox_TextChanged;
        }

        /// <summary>
        /// 文本框失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UiTextBox_LostFocus(object sender, EventArgs e)
        {
            UITextBox uITextBox = sender as UITextBox;

            uITextBox.TextChanged -= uiTextBox_TextChanged;
        }
       

        /// <summary>
        /// 清空TextBox的文本
        /// </summary>
        private void ClearTextBoxValue(UITextBox uITextBox)
        {
            foreach (var control in this.uiTableLayoutPanel.Controls)
            {
                if (!(control is UITextBox textBox)) continue;

                textBox.Text = string.Empty;
            }
        }

        /// <summary>
        /// 文本框值改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTextBox_TextChanged(object sender, EventArgs e)
        {
            UITextBox uITextBox = sender as UITextBox;

            if (string.IsNullOrEmpty(uITextBox.Text))
            {
                ClearTextBoxValue(uITextBox);
                return;
            }

            if (!double.TryParse(uITextBox.Text, out double value)) return;

            TimeConversion(uITextBox.TagString, value);

        }


        /// <summary>
        /// 时间换算
        /// </summary>
        private void TimeConversion(string type, double value)
        {
            TimeSpan timeSpan = new TimeSpan();

            switch (type)
            {
                case "年": timeSpan = TimeConverter.FromYears(value); break;
                case "周": timeSpan = TimeConverter.FromWeeks(value); break;
                case "天": timeSpan = TimeConverter.FromDays(value); break;
                case "时": timeSpan = TimeConverter.FromHours(value); break;
                case "分": timeSpan = TimeConverter.FromMinutes(value); break;
                case "秒": timeSpan = TimeConverter.FromSeconds(value); break;
                case "毫秒": timeSpan = TimeConverter.FromMilliseconds(value); break;
                case "微秒": timeSpan = TimeConverter.FromMicroseconds(value); break;
                case "纳秒": timeSpan = TimeConverter.FromNanoseconds(value); break;
            }

            if (this.uiTextBoxYear.TagString != type)
                this.uiTextBoxYear.Text = Math.Round(TimeConverter.ToYears(timeSpan), 7).ToString();

            if (this.uiTextBoxWeek.TagString != type)
                this.uiTextBoxWeek.Text = Math.Round(TimeConverter.ToWeeks(timeSpan), 7).ToString();

            if (this.uiTextBoxDay.TagString != type)
                this.uiTextBoxDay.Text = Math.Round(TimeConverter.ToDays(timeSpan), 7).ToString();

            if (this.uiTextBoxHour.TagString != type)
                this.uiTextBoxHour.Text = Math.Round(TimeConverter.ToHours(timeSpan), 7).ToString();

            if (this.uiTextBoxMinute.TagString != type)
                this.uiTextBoxMinute.Text = Math.Round(TimeConverter.ToMinutes(timeSpan), 7).ToString();

            if (this.uiTextBoxSecond.TagString != type)
                this.uiTextBoxSecond.Text = Math.Round(TimeConverter.ToSeconds(timeSpan), 7).ToString();

            if (this.uiTextBoxMillisecond.TagString != type)
                this.uiTextBoxMillisecond.Text = Math.Round(TimeConverter.ToMilliseconds(timeSpan), 7).ToString();

            if (this.uiTextBoxMicrosecond.TagString != type)
                this.uiTextBoxMicrosecond.Text = Math.Round(TimeConverter.ToMicroseconds(timeSpan), 7).ToString();

            if (this.uiTextBoxNanosecond.TagString != type)
                this.uiTextBoxNanosecond.Text = Math.Round(TimeConverter.ToNanoseconds(timeSpan), 7).ToString();
        }

    }
}
